package cn.iocoder.yudao.module.crm.service.performance.person;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.crm.controller.admin.performance.person.vo.PersonPerformanceConfigDetailPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.performance.CrmStatisticsPerformanceRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.performance.person.PersonPerformanceConfigDetailDO;
import cn.iocoder.yudao.module.crm.dal.mysql.performance.person.PersonPerformanceConfigDetailMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.statistics.CrmStatisticsPerformanceMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Function;

/**
 * CRM_业绩配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class PersonPerformanceConfigDetailServiceImpl implements PersonPerformanceConfigDetailService {

    @Resource
    private PersonPerformanceConfigDetailMapper personPerformanceConfigDetailMapper;

    @Resource
    private CrmStatisticsPerformanceMapper performanceMapper;


    @Override
    public PersonPerformanceConfigDetailDO getPerformanceConfigDetail(Long id) {
        return personPerformanceConfigDetailMapper.selectById(id);
    }

    @Override
    public PageResult<PersonPerformanceConfigDetailDO> getPerformanceConfigDetailPage(PersonPerformanceConfigDetailPageReqVO pageReqVO) {
        return personPerformanceConfigDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public String getResult(CrmStatisticsPerformanceReqVO performanceReqVO, String pointType){
        Function<CrmStatisticsPerformanceReqVO, List<CrmStatisticsPerformanceRespVO>> performanceFunction;
        switch (pointType) {
            case "1": //合同金额
                performanceFunction = performanceMapper::selectContractPricePerformance;
                List<CrmStatisticsPerformanceRespVO> performanceList = performanceFunction.apply(performanceReqVO);
                if(performanceList!=null && !performanceList.isEmpty()){
                    return performanceList.get(0).getCurrentMonthCount().toString();
                }else{
                    return "0";
                }
            case "2": // 回款金额
                performanceFunction = performanceMapper::selectReceivablePricePerformance;
                List<CrmStatisticsPerformanceRespVO> performanceList2 = performanceFunction.apply(performanceReqVO);
                if(performanceList2!=null && !performanceList2.isEmpty()){
                    return performanceList2.get(0).getCurrentMonthCount().toString();
                }else{
                    return "0";
                }
            case "3": // 拜访次数
                performanceFunction = performanceMapper::selectNumberOfVisitsPerformance;
                List<CrmStatisticsPerformanceRespVO> performanceList3 = performanceFunction.apply(performanceReqVO);
                if(performanceList3!=null && !performanceList3.isEmpty()){
                    return performanceList3.get(0).getCurrentMonthCount().toString();
                }else{
                    return "0";
                }
            case "4": // 跟进访户数
                performanceFunction = performanceMapper::selectNumberOfFollowUpCustomersPerformance;
                List<CrmStatisticsPerformanceRespVO> performanceList4 = performanceFunction.apply(performanceReqVO);
                if(performanceList4!=null && !performanceList4.isEmpty()){
                    return performanceList4.get(0).getCurrentMonthCount().toString();
                }else{
                    return "0";
                }
            default:
                return "系统未定义该类型业绩计算规则";
        }
    }
}